package com.me.regex;

import java.util.ArrayList;

public class Functions {
	
	public static String buildPattern(ArrayList<String> words) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("(");
		
		buffer.append( Functions.addSlashes( words.get(0)) );			
		
		for (int j = 1; j < words.size(); j++) { // 1 less than size
			buffer.append("|"+ Functions.addSlashes( words.get(j)) );
		}
		buffer.append(")");
		String titlePattern = buffer.toString();
		return titlePattern;
	}
	
	
	public static String addSlashes(String input) {

		return input.replaceAll("\\(", "\\\\(")
				.replaceAll("\\)", "\\\\)")
				.replaceAll("\\[", "\\\\[")
				.replaceAll("\\]", "\\\\]")
				.replaceAll("\\\\", "\\\\\\")
				.replaceAll("\\.", "\\\\.");
	}
}
